package  
{
	import flash.utils.ByteArray;
	import org.flixel.*;
	import flash.external.ExternalInterface;
	import spark.accessibility.DropDownListBaseAccImpl;
	
	/**
	 * ...
	 * @author Kyl
	 */
	public class LevelSelect extends FlxState 
	{
		public function LevelSelect(music:FlxSound)
		{
			super();
			gameInfo = new GameProgress(MAXLEVELS);
			bgMusic = music;
            
            SaveDataManager.load(gameInfo);
		}
        
		public function setGameProgress(info:GameProgress) : void 
		{
			gameInfo = info;
		}
		
		private const MAXLEVELS:int = 20;
		// Our only local variable is a current level object.
		// Eventually there should be a screen that chooses which
		// level gets loaded.
		private var gameInfo:GameProgress;
		private var curLevel:Level;
		private var buttons:Array;
		private var titleText:FlxText;
		private var ratingStar:FlxSprite;
		
		private var music:FlxSound;
        
        private var ROWS:Number = 10;
        private var COLUMNS:Number = 2;
		
		private var bgMusic:FlxSound;
		
		[Embed(source = "penCursor.png")]
		private var penCursor:Class;
		
		// This is where we will embed our XML level files.
		[Embed(source = "ExampleLevel.xml", mimeType = "application/octet-stream")]
		protected var EmbeddedXML:Class;
        
        [Embed(source = "level1.xml", mimeType = "application/octet-stream")]
		protected var Level_1:Class;
		
        [Embed(source = "level2.xml", mimeType = "application/octet-stream")]
		protected var Level_2:Class;
        
        [Embed(source = "level3.xml", mimeType = "application/octet-stream")]
		protected var Level_3:Class;
        
        [Embed(source = "level4.xml", mimeType = "application/octet-stream")]
		protected var Level_4:Class;
        
        [Embed(source = "level5.xml", mimeType = "application/octet-stream")]
		protected var Level_5:Class;
        
        [Embed(source = "level6.xml", mimeType = "application/octet-stream")]
		protected var Level_6:Class;
        
        [Embed(source = "level7.xml", mimeType = "application/octet-stream")]
		protected var Level_7:Class;
        
        [Embed(source = "level8.xml", mimeType = "application/octet-stream")]
		protected var Level_8:Class;
        
        [Embed(source = "level9.xml", mimeType = "application/octet-stream")]
		protected var Level_9:Class;
        
        [Embed(source = "level10.xml", mimeType = "application/octet-stream")]
		protected var Level_10:Class;
        
        [Embed(source = "level11.xml", mimeType = "application/octet-stream")]
		protected var Level_11:Class;
        
        [Embed(source = "level12.xml", mimeType = "application/octet-stream")]
		protected var Level_12:Class;
		
        [Embed(source = "level13.xml", mimeType = "application/octet-stream")]
		protected var Level_13:Class;
        
        [Embed(source = "level14.xml", mimeType = "application/octet-stream")]
		protected var Level_14:Class;
        
        [Embed(source = "level15.xml", mimeType = "application/octet-stream")]
		protected var Level_15:Class;
        
        [Embed(source = "level16.xml", mimeType = "application/octet-stream")]
		protected var Level_16:Class;
		
		[Embed(source = "level17.xml", mimeType = "application/octet-stream")]
		protected var Level_17:Class;
		
		[Embed(source = "level18.xml", mimeType = "application/octet-stream")]
		protected var Level_18:Class;
		
		[Embed(source = "level19.xml", mimeType = "application/octet-stream")]
		protected var Level_19:Class;
		
		[Embed(source = "level20.xml", mimeType = "application/octet-stream")]
		protected var Level_20:Class;
        
        [Embed(source = "level21.xml", mimeType = "application/octet-stream")]
		protected var Level_21:Class;
		
		[Embed(source = "level22.xml", mimeType = "application/octet-stream")]
		protected var Level_22:Class;
		
		private var ratingList:LevelRating;
		/*
        
        [Embed(source = "testlevel.xml", mimeType = "application/octet-stream")]
		protected var Test_level:Class;
		*/
		//[Embed(source = "levelTEST.xml", mimeType = "application/octet-stream")]
		//protected var Level_Test:Class;
        
		override public function create():void {
            FlxG.bgColor = 0xFF333366;
			if (FlxG.music)
				FlxG.music.stop();
			
			ratingList = new LevelRating();

			titleText = new FlxText(320, 10, 200, "Ink Master", false);
			titleText.setFormat("Comic Sans MS", 24, 0xFFF0F0F0, "center", 0);
			add(titleText);
			
			buttons = new Array();
			
			for ( var i:int = 0; i < COLUMNS; i++ )
			{
				for ( var j:int = 0; j < ROWS; j++ )
				{
                    var newButton:FlxIdxBtn = new FlxIdxBtn(200 + (i * 300), 70 + (j * 50), "Lvl " + (ROWS * i + (j + 1)), btnClick, (ROWS * i + j))
					newButton.loadGraphic(Resources.inGameButtonBG, false, false, 120, 40, false);
					newButton.label.offset.y = -8;
					newButton.label.setFormat(null, 12, 0xffffff, "center", 1);
					
					buttons.push(newButton);
					if ( gameInfo )
					{
						var level:int = ROWS * i + j;
                        if ( gameInfo.isLevelFinished(level) )
						{
							(FlxIdxBtn(buttons[level])).color = 0xFF66FF33;
							(FlxIdxBtn(buttons[level])).label.width = 120;
							(FlxIdxBtn(buttons[level])).label.text = "Lvl " + (level + 1) + ": " + gameInfo.getCount(ROWS * i + j);
                            ratingStar = new FlxSprite(180 + (i * 300), 80 + (j * 50));
							if (gameInfo.getCount(ROWS * i + j) >= ratingList.levelRating[level + 1][0]) {
								ratingStar.loadGraphic(Resources.gold, false, false, 60, 60, false);
							} else if (gameInfo.getCount(ROWS * i + j) >= ratingList.levelRating[level + 1][1]) {
								ratingStar.loadGraphic(Resources.silver, false, false, 60, 60, false);
							} else {
								ratingStar.loadGraphic(Resources.bronze, false, false, 60, 60, false);
							}
							ratingStar.scale.x = ratingStar.scale.y = 0.4;
							ratingStar.offset.x = 22;
							ratingStar.offset.y = 22;
							add(ratingStar);
						} else {
                            (FlxIdxBtn(buttons[level])).active = false;
							(FlxIdxBtn(buttons[level])).color = 0x5566FF33;
                            (FlxIdxBtn(buttons[level])).update();
                        }
						/*
                        if (level > 0 && gameInfo.isLevelFinished(level - 1)) {
                           (FlxIdxBtn(buttons[level])).active = true;
                           (FlxIdxBtn(buttons[level])).color = 0xFF66FF33;
                        }*/
					}
					
					add(buttons[i * ROWS + j]);
				}
			}
			var numFinished = gameInfo.getFurthestLevel() + 1;
			

			for (var i:int = 0; i < numFinished; i++) {
				if (i < MAXLEVELS) {
					(FlxIdxBtn(buttons[i])).active = true;
					(FlxIdxBtn(buttons[i])).color = 0xFFFFFFFF;
				}
			}
            //(FlxIdxBtn(buttons[0])).active = true;

			//(FlxIdxBtn(buttons[0])).color = 0xFFFFFFFF;
			FlxG.mouse.load(penCursor, 1, 0, 0);
			FlxG.mouse.show();
        }
        
		// Switch to a level based on button selected
		public function btnClick(idx:int): void
		{
			
			var levelLayout:XML;
			var ba:ByteArray;
            
			switch(idx + 1)
			{
				case 0:
					ba = new Level_1() as ByteArray;
					levelLayout = new XML(ba.readUTFBytes(ba.length));
					levelLayout.ignoreWhitespace = true;
					break;
				case 1:
					ba = new Level_2() as ByteArray;
					levelLayout = new XML(ba.readUTFBytes(ba.length));
					levelLayout.ignoreWhitespace = true;
					break;
				case 2:
					ba = new Level_3() as ByteArray;
					levelLayout = new XML(ba.readUTFBytes(ba.length));
					levelLayout.ignoreWhitespace = true;
					break;
				case 3:
					ba = new Level_4() as ByteArray;
					levelLayout = new XML(ba.readUTFBytes(ba.length));
					levelLayout.ignoreWhitespace = true;
					break;
				case 4:
					ba = new Level_5() as ByteArray;
					levelLayout = new XML(ba.readUTFBytes(ba.length));
					levelLayout.ignoreWhitespace = true;
					break;
				case 5:
					ba = new Level_6() as ByteArray;
					levelLayout = new XML(ba.readUTFBytes(ba.length));
					levelLayout.ignoreWhitespace = true;
					break;
				case 6:
					ba = new Level_7() as ByteArray;
					levelLayout = new XML(ba.readUTFBytes(ba.length));
					levelLayout.ignoreWhitespace = true;
					break;
				case 7:
					ba = new Level_8() as ByteArray;
					levelLayout = new XML(ba.readUTFBytes(ba.length));
					levelLayout.ignoreWhitespace = true;
					break;
				case 8:
					ba = new Level_9() as ByteArray;
					levelLayout = new XML(ba.readUTFBytes(ba.length));
					levelLayout.ignoreWhitespace = true;
					break;
				case 9:
					ba = new Level_10() as ByteArray;
					levelLayout = new XML(ba.readUTFBytes(ba.length));
					levelLayout.ignoreWhitespace = true;
					break;
                case 10:
					ba = new Level_11() as ByteArray;
					levelLayout = new XML(ba.readUTFBytes(ba.length));
					levelLayout.ignoreWhitespace = true;
					break;
				case 11:
					ba = new Level_12() as ByteArray;
					levelLayout = new XML(ba.readUTFBytes(ba.length));
					levelLayout.ignoreWhitespace = true;
					break;
                case 12:
					ba = new Level_13() as ByteArray;
					levelLayout = new XML(ba.readUTFBytes(ba.length));
					levelLayout.ignoreWhitespace = true;
					break;
                case 13:
					ba = new Level_14() as ByteArray;
					levelLayout = new XML(ba.readUTFBytes(ba.length));
					levelLayout.ignoreWhitespace = true;
					break;
                case 14:
					ba = new Level_15() as ByteArray;
					levelLayout = new XML(ba.readUTFBytes(ba.length));
					levelLayout.ignoreWhitespace = true;
					break;
                case 15:
					ba = new Level_16() as ByteArray;
					levelLayout = new XML(ba.readUTFBytes(ba.length));
					levelLayout.ignoreWhitespace = true;
					break;
				case 16:
					ba = new Level_17() as ByteArray;
					levelLayout = new XML(ba.readUTFBytes(ba.length));
					levelLayout.ignoreWhitespace = true;
					break;
				case 17:
					ba = new Level_18() as ByteArray;
					levelLayout = new XML(ba.readUTFBytes(ba.length));
					levelLayout.ignoreWhitespace = true;
					break;
				case 18:
					ba = new Level_19() as ByteArray;
					levelLayout = new XML(ba.readUTFBytes(ba.length));
					levelLayout.ignoreWhitespace = true;
					break;
				case 19:
					ba = new Level_20() as ByteArray;
					levelLayout = new XML(ba.readUTFBytes(ba.length));
					levelLayout.ignoreWhitespace = true;
					break;
                case 20:
					ba = new Level_21() as ByteArray;
					levelLayout = new XML(ba.readUTFBytes(ba.length));
					levelLayout.ignoreWhitespace = true;
					break;
				default:
					ba = new EmbeddedXML() as ByteArray;
					levelLayout = new XML(ba.readUTFBytes(ba.length));
					levelLayout.ignoreWhitespace = true;
						
			}
			curLevel = new Level(levelLayout, idx, gameInfo, this, bgMusic);
			
			FlxG.switchState(curLevel);
		}
	}
}